package com.zqxq.common.constant;

import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "响应码", required = true)
    private int code = CodeEnum.SUCCESS.getCode();//响应码，默认成功

    @ApiModelProperty(value = "响应消息", required = true)
    private String msg = CodeEnum.SUCCESS.getMsg();//响应消息

    @ApiModelProperty(value = "结果对象", required = true)
    private T data = null;//结果对象

    @ApiModelProperty(hidden=true)
    private Boolean success;

    @ApiModelProperty(hidden=true)
    private Boolean fail;

    public Result() {
    }

    public Boolean success(){

        return CodeEnum.SUCCESS.getCode()==code;
    }

    public static <T> Result<T> OK(){
        return new Result<>(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg());
    }

    public static <T> Result<T> OK(String msg){
        return new Result<>(CodeEnum.SUCCESS.getCode(),msg);
    }

    public static <T> Result<T> OK(T data){
        return new Result<>(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg(),data);
    }

    public static <T> Result<T> Error(){
        return new Result<>(CodeEnum.FAIL.getCode(),CodeEnum.FAIL.getMsg());
    }

    public static <T> Result<T> Error(String msg){
        return new Result<>(CodeEnum.FAIL.getCode(),msg);
    }

    public Boolean isFail(){

        return CodeEnum.SUCCESS.getCode()!=code;
    }
    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
